package bhbt.controller.member.member;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bhbt.controller.core.BaseController;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.TelValService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class MemberController extends BaseController {
    @Resource
    private MemberService memberService;
    @Resource
	private DailiService dailiService;
    @Resource
	private TelValService telValService;
    
    
    @Resource
	private ParamWageService paramWageService;
    
    
    /**
     * 注册show
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/memberAddShow.shtml")
	public String list(HttpServletRequest request) throws Exception{
		
		//获取list
		Map formMap = getParameterMap(request);
		
		Member member = ContextUtil.getMember();
		request.setAttribute("recommendLoginName",member.getLoginName());
		
		List dailiList = dailiService.queryForList("select", formMap);
		request.setAttribute("dailiList",dailiList);
		
		
		//接点图传过来的参数
		String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
		String qu = MapUtil.getString(formMap, "qu");
		
		request.setAttribute("qu",qu);
		request.setAttribute("fatherLoginName",fatherLoginName);
		
		return "member/member/memberAdd";
	}

	/**
     * 注册
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/memberAdd.shtml")
    public String add(ModelMap model,HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map formMap = getParameterMap(request);
		Member regist = ContextUtil.getMember();
		/*****************验证******************/
		
		//注册人
		Member register = ContextUtil.getMember();
		boolean result = true;
		//账号
		String loginName = MapUtil.getString(formMap, "loginName");
		if(loginName.length()<6){
			setMessage(model,"账户必须大于6位。");
			result = false;
		}
		if(memberService.executMemberByLoginName(loginName)!=null){
			setMessage(model,"该会员账号已经存在。");
			result = false;
		}
		
		String regEx = "[\u4e00-\u9fa5]"; 
		Pattern pat = Pattern.compile(regEx); 
		Matcher matcher = pat.matcher(loginName); 
		if (matcher.find()){ 
			setMessage(model,"登录名不能包含中文。");
			result = false;
			 
		} 
		//过滤登录名的特殊字符
		if(loginName.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length()!=0){
		 
			setMessage(model,"该登录名含有特殊字符，请重新填写！");
		} 
		
		if(loginName.length()>loginName.replaceAll(" ", "").length()){
			setMessage(model,"该登录名含有空格，请重新填写！");
		}
		 
 
		
		String wei= MapUtil.getString(formMap,"wei");
		String qq= MapUtil.getString(formMap,"qq");
		if(wei.equals("")||wei.equals("")){
			//setMessage(model,"请输入微信号码。");
			//result = false;
		}
		if(qq.equals("")||qq.equals("")){
			//setMessage(model,"请输入QQ号码。");
			//result = false;
		}
 
		
		
		//推荐人
		String recommendLoginName = MapUtil.getString(formMap, "recommendLoginName");
		 
		Integer recommendId = null;
		Member recommend = null;
		if(!recommendLoginName.equals("")&&recommendLoginName!=null){
			recommend = memberService.executMemberByLoginName(recommendLoginName);
			if(recommend != null){
				if(recommend.getAccountActive().equals("F")){
					setMessage(model,"推荐人未激活。");
					result=false;
				}else{
					recommendId = recommend.getId();
				} 
			}else{
				setMessage(model,"推荐人不存在。");
				result = false;
				 
			}
		}else {
			recommend = regist;
			recommendId = regist.getId();
			 
		}
		
		
		
		  
		
		if(!result){
			
			request.setAttribute("recommendLoginName",regist.getLoginName());
			List dailiList = dailiService.queryForList("select", formMap);
			request.setAttribute("dailiList",dailiList);
			return "member/member/memberAdd";
		}
		String onePsw= MapUtil.getString(formMap,"onePassword");
		if(onePsw.length()<6){
			setMessage(model,"一级密码必须大于6位。");
			result = false;
		}
		
		String onePassword = CryptUtil.getInstance().encrypt2String(onePsw,CryptUtil.keyStr);
		
		String twoPsw= MapUtil.getString(formMap,"twoPassword");
		if(twoPsw.length()<6){
			setMessage(model,"二级密码必须大于6位。");
			result = false;
		}
		String twoPassword = CryptUtil.getInstance().encrypt2String(twoPsw,CryptUtil.keyStr);
		
		 
		Member one = new Member();
		 
		one.setLoginName(loginName);
		one.setOnePassword(onePassword);
		one.setTwoPassword(twoPassword);
		one.setRecommendId(recommendId);
		one.setRegisterId(register.getId());
		 
		ParamWage paramWage1003= this.paramWageService.getByKey(1003);
		 
		
		one.setDailiId(10);
		one.setPayNum(paramWage1003.getValue());
		one.setCardNo(MapUtil.getString(formMap,"cardNo"));
		one.setRemark(MapUtil.getString(formMap,"remark"));
		one.setTel(MapUtil.getString(formMap,"tel"));
		one.setPersonName(MapUtil.getString(formMap,"personName"));
		one.setWei(MapUtil.getString(formMap,"wei"));
		one.setQq(MapUtil.getString(formMap,"qq"));
		
		one.setBankShi(MapUtil.getString(formMap, "bankShi"));
		one.setBankName(MapUtil.getString(formMap, "bankName"));
		one.setBankAddress(MapUtil.getString(formMap, "bankAddress"));
		one.setBankCardNo(MapUtil.getString(formMap, "bankCardNo"));
		one.setBankSheng(MapUtil.getString(formMap, "bankSheng"));
		one.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
		 
		 
		 
	 
		memberService.saveOne(one);
		
		return "redirect:memberNoActiveList.shtml";
		
    } 
	
	/**
     * 注册ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/ajaxGetRecommendByName.shtml")
	@ResponseBody
	public void ajaxTuRecommendAction( HttpServletRequest request, HttpServletResponse response) {
		 
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String recommendLoginName= MapUtil.getString(formMap,"recommendLoginName");
		Member member=memberService.executMemberByLoginName(recommendLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null){
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
		
	}
	/**
     * 注册ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/ajaxGetFatherByName.shtml")
	@ResponseBody
	public void ajaxTuFahterAction( HttpServletRequest request, HttpServletResponse response) {
		 
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String fatherLoginName= MapUtil.getString(formMap,"fatherLoginName");
		Member member=memberService.executMemberByLoginName(fatherLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null){
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
		
	}
	
	/**
     * 注册ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/ajaxGetCenterByName.shtml")
	@ResponseBody
	public void ajaxTuCenterAction( HttpServletRequest request, HttpServletResponse response) {
		 
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String centerLoginName= MapUtil.getString(formMap,"centerLoginName");
		Member member=memberService.executMemberByLoginName(centerLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null&&member.getAccountCenter().equals("T")){
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
		
	}
	
	/**
     * loginName检测  ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("member/member/ajaxGetMemberByName.shtml")
	@ResponseBody
	public void ajaxGetMemberByName( HttpServletRequest request, HttpServletResponse response) {
		
		 
		
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		 
		HashMap resultformMap = new HashMap();
		if(member==null){
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			renderSuccess(response, false, resultformMap); 
			
		}
		
	}
	
	
	 
    /**
     * 激活显示
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("member/member/memberActiveShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        Member active = ContextUtil.getMember();
    	
    	active = memberService.getByKey(active.getId());
    	
    	 
    		
		request.setAttribute("active", active);
        List<Member> list = memberService.queryForList("memberForList",formMap);
    	
    	if(list.size()>0){
    		System.out.println("list.get(0):"+list.get(0));
        	 
        	request.setAttribute("member", list.get(0));
    	}
    	return "member/member/memberActive";
	 
    
       
    }
    
    /**
     * 激活
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("member/member/memberActive.shtml")
    public String update(HttpServletRequest request, ModelMap model) throws Exception{
        
    	 
    	Map formMap = getParameterMap(request);
        
        String twoPassword = MapUtil.getString(formMap, "twoPassword");
        String activeLoginName = MapUtil.getString(formMap, "activeLoginName");
        String loginName = MapUtil.getString(formMap, "loginName");
        Member member = memberService.executMemberByLoginName(activeLoginName);
        
        boolean result =true;  //验证结果
        if(member!=null){
        	
        	//激活人
        	Member active = ContextUtil.getMember();
        	//要激活的会员
        	
        	if(!member.getLoginName().equals(loginName)){
        		setMessage(model,"会员账号不正确。");
        		result=false;
        	} 
        	if(member.getAccountActive().equals("F")){
        		 
        		if(!member.getRecommendId().equals(active.getId())
        			&&!member.getRegisterId().equals(active.getId())&&!member.getCenterId().equals(active.getId())){
        			setMessage(model,"该会员不在您的团队。");
            		result=false;
        		}else {
        			String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
                	if(!active.getTwoPassword().equals(pwd)){
                		setMessage(model,"安全密码不正确。");
                		result=false;
                	}else{
                		//推荐人验证
                		if(member.getRecommendId()!=null){
                			Member recommend = memberService.getByKey(member.getRecommendId());
                			if(recommend!=null){
                				if(recommend.getAccountActive().equals("F")){
                					setMessage(model,"推荐人未激活。");
                					result=false;
                				}
                			}
                			 
                		}
                		
                		 
                		
                		//币的验证
        				
        				if(member.getPayNum()>active.getMoney()){
        					setMessage(model,"现金币不足："+member.getPayNum());
        					result=false;
        				}
        			} 
				}
        		
        		
            	
        	}else {
        		setMessage(model,"会员已经激活");
				result=false;
			}
        	
        	if(!result){
        		Map<String, Object> memberInfo = memberService.queryForObject("getJoinByKey", member.getId());
            	
            	request.setAttribute("member", memberInfo); 
            	
            	active = memberService.getByKey(active.getId());
            	request.setAttribute("active", active);
        		return "member/member/memberActive";
    		}
        	
        	
        	 
        	memberService.saveActive(member.getId(),active.getId());
        	
        	 
        }
        return "redirect:memberYesActiveList.shtml";
    }

     
}
